﻿using System;
using System.Collections.Generic;
using System.Linq;
using DomenModel;
using System.Linq.Expressions;
using Model.Repositories.Interfaces;
using System.Data.Entity;

namespace OmsTestProject.MockRepository
{
    public class RoleRepository : ICrud<Role>
    {
        MockRepositoryContext _db = MockRepositoryContext.MockContextInstance;

        public Role GetById(long Id)
        {
                Role roleById = _db.Roles.FirstOrDefault(p => p.RoleId == Id);
                return roleById;
        }

        public Role GetByName(string Name)
        {
                Role roleByName = _db.Roles.FirstOrDefault(p => p.Name == Name);
                return roleByName;
        }

          public void Save(Role role)
          {
                  _db.Roles.Add(new Role
                  {
                      Name = role.Name,
                      Description = role.Description,

                  });
          }

          public void Update(Role role)
          {
                  
                  _db.Roles[role.RoleId].Name = role.Name;
                  _db.Roles[role.RoleId].Description = role.Description;
          }

          public void Delete(long Id)
          {
                  Role roleById = _db.Roles.First(p => p.RoleId == Id);
                  _db.Roles.Remove(roleById);
          } 

        public IQueryable<Role> GetCollectionByFilter(Expression<Func<Role, bool>> predicate)
        {
                return _db.Roles.AsQueryable<Role>().Where(predicate);
        } 

        public List<Role> GetCollection()
        {
                return _db.Roles;
        }
    }
}
